package com.fbinggun.sql;

import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * MYSQL 数据库 请使用SqlUtil类创建
 */
public class MySql extends SQLite{

    public MySql(String hostname, String username, String password, String dataname, boolean useSSL, JavaPlugin Plugin) throws SQLException, ClassNotFoundException {
       super(Plugin);
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.dataname = dataname;
        this.useSSL = useSSL;
       if(connection()==null){
               IsNull=false;
           Plugin.getLogger().warning("数据库连接,错请检查数据库");
       }
    }

    public MySql(String hostname, String username, String password, String dataname, String additional, JavaPlugin Plugin) throws SQLException, ClassNotFoundException {
        super(Plugin);
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.dataname = dataname;
        this.additional = additional;
        if(connection()==null){
            IsNull=false;
            Plugin.getLogger().warning("数据库连接,错请检查数据库");
        }
    }


    public MySql(String hostname, String username, String password, String dataname, String additional ) throws SQLException, ClassNotFoundException {
        super();
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.dataname = dataname;
        this.additional = additional;
        if(connection()==null){
            IsNull=false;
        }
    }


    public MySql(String hostname, String username, String password, String dataname, boolean useSSL) throws SQLException, ClassNotFoundException {
        super();
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.dataname = dataname;
        this.useSSL = useSSL;
        if(connection()==null){
            IsNull=false;
        }
    }

    public Connection connection;
    //数据库
    public Statement statement;
    public  boolean IsNull=false;

    //数据库地址
     private String hostname;
    //用户名
    private String username;
    //密码
    private String password;
    //数据库名
    String dataname;
    public  boolean useSSL =false;
    String additional;
    @Override
    public Connection getConnections() throws SQLException, ClassNotFoundException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        if(additional!=null) {
            connection = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + dataname + "?"+additional, username, password);

        }else {
            connection = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + dataname + "?useSSL=" + useSSL + "&autoReconnect=true", username, password);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connection;
}


    //关闭此数据库连接
    @Override
    public void CloseSql() throws SQLException {
        statement.close();
        connection.close();
    }
    //连接数据库
    @Override
    public Statement connection() throws SQLException, ClassNotFoundException {
        connection=  getConnections();
      this.statement = connection.createStatement();
       return statement;
    }
    //判断数据库是否是链接状态
    @Override
    public boolean IsConnection() throws SQLException {
        return statement.isClosed();
    }


    @Override
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        return connection;
    }
    @Override
    public Statement getStatement() throws SQLException {
        return connection.createStatement();
    }

}
